﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_HMS.Classes;

namespace Delonix_Regia_HMS
{
    public partial class CustomerCheckOut : Form
    {
        public CustomerCheckOut()
        {
            InitializeComponent();
            DBManager.DisplayAllItems(listViewCustomerCheckOutItem);
            lblCustomerCheckOutTodayDate.Text = DateTime.Now.ToString();
        }

        int checkInId = 0;
        double itemUnitPrice = 0.0;
        double totalRoomPrice = 0.0;
        double additionalCost = 0.0;
        double grandTotal = 0.0;
        DateTime actualCheckOutDate = DateTime.MinValue;


        private void onlyNumbers_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void OnlyCharacters_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
        }

        private void btnCustomerCheckOutSearchByCheckInId_Click(object sender, EventArgs e)
        {
            tbxCustomerCheckOutSearchByCustomerName.Clear();
            tbxCustomerCheckOutSearchByNric.Clear();
            if (string.IsNullOrEmpty(tbxCustomerCheckOutSearchByCheckInId.Text))
            {
                MessageBox.Show("Please enter a CheckIn Id to search!");
            }
            else
            {
                DBManager.DisplayAllCheckInByCheckInId(listViewCustomerCheckOutCheckInInformation, Convert.ToInt32(tbxCustomerCheckOutSearchByCheckInId.Text));
            }
        }

        private void btnbtnCustomerCheckOutSearchByCustomerNric_Click(object sender, EventArgs e)
        {
            tbxCustomerCheckOutSearchByCheckInId.Clear();
            tbxCustomerCheckOutSearchByCustomerName.Clear();
            if (string.IsNullOrEmpty(tbxCustomerCheckOutSearchByNric.Text))
            {
                MessageBox.Show("Please enter a customer NRIC to search!");
            }
            else
            {
                DBManager.DisplayAllCheckInByCustomerNric(listViewCustomerCheckOutCheckInInformation, tbxCustomerCheckOutSearchByNric.Text);
            }
        }

        private void btnbtnCustomerCheckOutSearchByCustomerName_Click(object sender, EventArgs e)
        {
            tbxCustomerCheckOutSearchByCheckInId.Clear();
            tbxCustomerCheckOutSearchByNric.Clear();
            if (string.IsNullOrEmpty(tbxCustomerCheckOutSearchByCustomerName.Text))
            {
                MessageBox.Show("Please enter a customer first name");
            }
            else
            {
                DBManager.DisplayAllCheckInByCustomerFirstName(listViewCustomerCheckOutCheckInInformation, tbxCustomerCheckOutSearchByCustomerName.Text);
            }
        }

        private void btnCustomerCheckOutDisplayAllCheckIn_Click(object sender, EventArgs e)
        {
            tbxCustomerCheckOutSearchByCheckInId.Clear();
            tbxCustomerCheckOutSearchByCustomerName.Clear();
            tbxCustomerCheckOutSearchByNric.Clear();
            DBManager.DisplayAllCheckIn(listViewCustomerCheckOutCheckInInformation);
        }

        private void listViewCustomerCheckOutCheckInInformation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewCustomerCheckOutCheckInInformation.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in listViewCustomerCheckOutCheckInInformation.SelectedItems)
                {
                    checkInId = Convert.ToInt32(listViewCustomerCheckOutCheckInInformation.SelectedItems[0].Text);
                    tbxCustomerCheckOutCheckInId.Text = listViewCustomerCheckOutCheckInInformation.SelectedItems[0].Text;
                }
            }
            DateTime actualCheckInDate = DBManager.GetCheckInDate(checkInId);
            actualCheckOutDate = DateTime.Now;
            TimeSpan totalDaysStayed = actualCheckOutDate - actualCheckInDate;
            int iTotalDaysStayed = Convert.ToInt32(totalDaysStayed.TotalDays);
            tbxCustomerCheckOutTotalDaysStayed.Text = iTotalDaysStayed.ToString();
            double roomRates = DBManager.GetRoomRates(checkInId);
            totalRoomPrice = iTotalDaysStayed * roomRates;
            tbxCustomerCheckOutTotalRoomPrice.Text = totalRoomPrice.ToString();
            tbxCustomerCheckOutNric.Text = DBManager.GetCustomerNric(checkInId);
        }

        private void listViewCustomerCheckOutItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewCustomerCheckOutItem.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in listViewCustomerCheckOutItem.SelectedItems)
                {
                    tbxAddItemsConsumed.Text = listViewCustomerCheckOutItem.SelectedItems[0].Text;
                    itemUnitPrice = Convert.ToDouble(listViewCustomerCheckOutItem.SelectedItems[0].SubItems[1].Text);
                }
            }
        }

        private void btnItemsConsumedAddItem_Click(object sender, EventArgs e)
        {
            bool successfulCreate = false;
            if (DBManager.CheckForItemsConsumed(tbxAddItemsConsumed.Text, checkInId) == true)
            {
                MessageBox.Show("Sorry the item already existed inside the database, please use the update item button to change the quantity!");
            }
            else if (string.IsNullOrEmpty(tbxAddItemsConsumed.Text))
            {
                MessageBox.Show("Please select an item to charge!");
            }
            else if (string.IsNullOrEmpty(tbxAddItemQuantity.Text))
            {
                MessageBox.Show("Please enter a quantity for that item!");
            }
            else if (checkInId == 0)
            {
                MessageBox.Show("Please select a Check In to charge for items consumed!");
            }
            else
            {
                ItemInvoice i = new ItemInvoice();
                i.ItemName = tbxAddItemsConsumed.Text;
                i.Quantity = Convert.ToInt32(tbxAddItemQuantity.Text);
                i.CheckInId = checkInId;
                DBManager.CreateItemsConsumed(i);
                successfulCreate = true;
            }

            if (successfulCreate == true)
            {
                MessageBox.Show("Insert Item Consumed Successful!");
                DBManager.DisplayAllItemsConsumeByCheckInId(listViewCustomerCheckOutItemsConsumed, checkInId);
                tbxAddItemQuantity.Clear();
                tbxAddItemsConsumed.Clear();
            }
            else
            {
                MessageBox.Show("Insert Item Consumed NOT Successful");
            }
        }

        private void btnItemsConsumedUpdateItem_Click(object sender, EventArgs e)
        {
            bool successfulUpdate = false;
            if (DBManager.CheckForItemsConsumed(tbxUpdateItemsConsumed.Text, checkInId) == false)
            {
                MessageBox.Show("Sorry the item does not existed inside the database, please use the add item button to charge the customer for items consumed!");
            }
            else if (string.IsNullOrEmpty(tbxUpdateItemsConsumed.Text))
            {
                MessageBox.Show("Please select an item to update!");
            }
            else if (string.IsNullOrEmpty(tbxUpdateItemQuantity.Text))
            {
                MessageBox.Show("Please enter a quantity for that item to update!");
            }
            else if (checkInId == 0)
            {
                MessageBox.Show("Please select a check in to update the items consumed!");
            }
            else
            {
                ItemInvoice i = new ItemInvoice();
                i.ItemName = tbxUpdateItemsConsumed.Text;
                i.Quantity = Convert.ToInt32(tbxUpdateItemQuantity.Text);
                i.CheckInId = checkInId;
                DBManager.UpdateItemsConsumed(i);
                successfulUpdate = true;
            }

            if (successfulUpdate == true)
            {
                listViewCustomerCheckOutItemsConsumed.Items.Clear();
                MessageBox.Show("Update Item Consumed Successful!");
                DBManager.DisplayAllItemsConsumeByCheckInId(listViewCustomerCheckOutItemsConsumed, checkInId);
                tbxUpdateItemQuantity.Clear();
                tbxUpdateItemsConsumed.Clear();
            }
            else
            {
                MessageBox.Show("Update Item Consumed NOT Successful");
            }
        }

        private void listViewCustomerCheckOutItemsConsumed_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewCustomerCheckOutItemsConsumed.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in listViewCustomerCheckOutItemsConsumed.SelectedItems)
                {
                    tbxUpdateItemsConsumed.Text = listViewCustomerCheckOutItemsConsumed.SelectedItems[0].Text;
                    tbxUpdateItemQuantity.Text = listViewCustomerCheckOutItemsConsumed.SelectedItems[0].SubItems[1].Text;
                }
            }
        }

        //Proceed to Payment
        private void btntbxCustomerCheckOutConfirmCheckOut_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbxCustomerCheckOutCheckInId.Text))
            {
                MessageBox.Show("Please select a Check In Id to check out!");
            }
            else if(string.IsNullOrEmpty(tbxCustomerCheckOutActualCheckOutTime.Text))
            {
                MessageBox.Show("Please enter an Actual Check out time");
            }
            else if (Convert.ToInt32(tbxCustomerCheckOutActualCheckOutTime.Text) > 2359 || Convert.ToInt32(tbxCustomerCheckOutActualCheckOutTime.Text) < 0000)
            {
                MessageBox.Show("Please Enter the Actual Check Out Time in 24 hour format from 0000(12am)  to 2359(11.59pm).");
            }
            else if (string.IsNullOrEmpty(tbxCustomerCheckOutGrandTotal.Text))
            {
                MessageBox.Show("Please enter the Calculate Cost Button to calculate the grand total!");
            }
            else
            {
                if (string.IsNullOrEmpty(tbxCustomerCheckOutAdditionalCost.Text))
                {
                    additionalCost = 0.0;
                    Payment p = new Payment(Convert.ToInt32(tbxCustomerCheckOutCheckInId.Text), tbxCustomerCheckOutNric.Text, actualCheckOutDate, tbxCustomerCheckOutActualCheckOutTime.Text, Convert.ToInt32(tbxCustomerCheckOutTotalDaysStayed.Text), additionalCost, Convert.ToDouble(tbxCustomerCheckOutTotalRoomPrice.Text), Convert.ToDouble(tbxCustomerCheckOutGrandTotal.Text));
                    p.ShowDialog();
                }
                else
                {
                    Payment p = new Payment(Convert.ToInt32(tbxCustomerCheckOutCheckInId.Text), tbxCustomerCheckOutNric.Text, actualCheckOutDate, tbxCustomerCheckOutActualCheckOutTime.Text, Convert.ToInt32(tbxCustomerCheckOutTotalDaysStayed.Text), Convert.ToDouble(tbxCustomerCheckOutAdditionalCost.Text), Convert.ToDouble(tbxCustomerCheckOutTotalRoomPrice.Text), Convert.ToDouble(tbxCustomerCheckOutGrandTotal.Text));
                    p.ShowDialog();
                }
            }
        }

        private void dtpCustomerCheckOutActualCheckOutDate_ValueChanged(object sender, EventArgs e)
        {
            DateTime actualCheckInDate = DBManager.GetCheckInDate(checkInId);
            DateTime actualCheckOutDate = DateTime.Now;
            TimeSpan totalDaysStayed = actualCheckOutDate - actualCheckInDate;
            tbxCustomerCheckOutTotalDaysStayed.Text = totalDaysStayed.TotalDays.ToString();
        }

        private void btnCalculateAddtionalCost_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(tbxCustomerCheckOutCheckInId.Text))
            {
                MessageBox.Show("Please Select An existing Check In to Check Out!");
            }
            else if (listViewCustomerCheckOutItemsConsumed.Items.Count == 0)
            {
                grandTotal = Convert.ToDouble(tbxCustomerCheckOutTotalRoomPrice.Text);
                tbxCustomerCheckOutGrandTotal.Text = grandTotal.ToString();
            }
            else
            {
                int size = 0;
                if (listViewCustomerCheckOutItemsConsumed.Items.Count > 0)
                {
                    foreach (ListViewItem item in listViewCustomerCheckOutItemsConsumed.Items)
                    {
                        size++;
                    }
                }
                for (int i = 0; i < size; i++)
                {
                    additionalCost = DBManager.CalculateTotalItemCost(checkInId);
                }
                tbxCustomerCheckOutAdditionalCost.Text = additionalCost.ToString();
                grandTotal = Convert.ToDouble(tbxCustomerCheckOutAdditionalCost.Text) + Convert.ToDouble(tbxCustomerCheckOutTotalRoomPrice.Text);
                tbxCustomerCheckOutGrandTotal.Text = grandTotal.ToString();
            }
        }
    }
}
